package com.google.code.greenplate;

import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyLocationMap extends MapActivity {
	   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        // Adds controls to map
        MapView mapView = (MapView) findViewById(R.id.map);  
        mapView.setBuiltInZoomControls(true);        
        
        // Get lat, lng
        int lat = 0;
        int lng = 0;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lat = extras.containsKey("Lat") ? extras.getInt("Lat") : null; 
			lng = extras.containsKey("Lng") ? extras.getInt("Lng") : null; 
		}

        // Create a list of overlays
        MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(getResources().getDrawable(R.drawable.marker1));
        
        // Add an overlay using integers for lat,lng and drawing the same Drawable
        GeoPoint point = new GeoPoint(lat, lng);
        OverlayItem overlayitem = new OverlayItem(point, "My Location", "Here I am.");
        itemizedoverlay.addOverlay(overlayitem);

        mapView.getOverlays().add(itemizedoverlay); 
        mapView.postInvalidate();         
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	
	private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
		
		// Keep track of overlay item
		private ArrayList<OverlayItem> myOverlays = new ArrayList<OverlayItem>();
		
		public MyItemizedOverlay(Drawable defaultMarker) {  
			super(boundCenterBottom(defaultMarker));
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			return myOverlays.get(i);
		}
		
		public void addOverlay(OverlayItem overlay) {    
			myOverlays.add(overlay);    
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);			
		}
		
		@Override
		protected boolean onTap(int i) {
			Toast.makeText(MyLocationMap.this, myOverlays.get(i).getSnippet(), Toast.LENGTH_LONG).show();
			return(true);
		} 
		
		@Override
		public int size() {
			return(myOverlays.size());
		}
	}
	
}
